﻿Public Class frmLoiNhuan

    Private Sub btnThongKe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThongKe.Click
        Dim th As TapHoa = New TapHoa()
        If cbxThang.SelectedIndex = 0 Then
            th.m_strThang = 20
        Else
            th.m_strThang = Integer.Parse(cbxThang.SelectedItem)
        End If
        th.m_strNam = Integer.Parse(cbxNam.SelectedItem)
        If txtTenSanPham.Text.Length = 0 Then
            th.m_strTenHangHoa = 1
        Else
            th.m_strTenHangHoa = txtTenSanPham.Text
        End If
        dgvSanPhamBanRa.DataSource = TapHoaDAO.ThongKeDoanhThu(th)
        If dgvSanPhamBanRa.Rows.Count = 1 Then
            MessageBox.Show("Không có doanh thu trong thời gian này !")
            cbxThang.Focus()
        Else
            Dim row As Integer = 0
            Dim TongTien As Integer = 0
            Dim SoLuong As Integer = 0
            While row < dgvSanPhamBanRa.Rows.Count - 1
                TongTien = TongTien + Integer.Parse(dgvSanPhamBanRa.Rows(row).Cells(2).Value.ToString())
                SoLuong = SoLuong + Integer.Parse(dgvSanPhamBanRa.Rows(row).Cells(1).Value.ToString())
                row = row + 1
            End While
            lbSoLuongNhap.Text = SoLuong.ToString()
            lbTongTienNhap.Text = TongTien.ToString()
        End If

        Dim th1 As TapHoa = New TapHoa()
        If cbxThang.SelectedIndex = 0 Then
            th1.m_strThang = 20
        Else
            th1.m_strThang = Integer.Parse(cbxThang.SelectedItem)
        End If
        th1.m_strNam = Integer.Parse(cbxNam.SelectedItem)
        If txtTenSanPham.Text.Length = 0 Then
            th1.m_strTenHangHoa = 1
        Else
            th1.m_strTenHangHoa = txtTenSanPham.Text
        End If
        dgvSanPhamNhapVe.DataSource = TapHoaDAO.ThongKeLoiNhuan(th1)
        If dgvSanPhamBanRa.Rows.Count = 1 Then
            MessageBox.Show("Không có doanh thu trong thời gian này !")
            cbxThang.Focus()
        Else
            Dim row As Integer = 0
            Dim TongTien As Integer = 0
            Dim SoLuong As Integer = 0
            While row < dgvSanPhamNhapVe.Rows.Count - 1
                TongTien = TongTien + Integer.Parse(dgvSanPhamNhapVe.Rows(row).Cells(2).Value.ToString())
                SoLuong = SoLuong + Integer.Parse(dgvSanPhamNhapVe.Rows(row).Cells(1).Value.ToString())
                row = row + 1
            End While
            lbSoLuongBan.Text = SoLuong.ToString()
            lbTongTienBan.Text = TongTien.ToString()
        End If
    End Sub

    Private Sub frmLoiNhuan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbxThang.Items.Add("Tất cả")
        Dim i As Integer = 1
        Dim j As Integer = 2010
        While i <= 12
            cbxThang.Items.Add(i)
            i = i + 1
        End While
        While j <= 2030
            cbxNam.Items.Add(j)
            j = j + 1
        End While
        cbxThang.SelectedIndex = 0
        cbxNam.SelectedIndex = 0
    End Sub
End Class